package com.toulezu.test.config;

import com.toulezu.test.entity.User;
import com.toulezu.test.redis.serializer.GzipObjectSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setDatabase(0);
        jedisConnectionFactory.setHostName("10.2.35.54");
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setPassword("redispass");
        jedisConnectionFactory.setTimeout(0);

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxWaitMillis(-1);
        poolConfig.setMaxIdle(8);
        poolConfig.setMinIdle(0);

        jedisConnectionFactory.setPoolConfig(poolConfig);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, User> redisTemplate() {
        RedisTemplate<String, User> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());

        template.setKeySerializer(new GzipObjectSerializer());
        template.setHashKeySerializer(new GzipObjectSerializer());

        template.setValueSerializer(new GzipObjectSerializer());
        template.setHashValueSerializer(new GzipObjectSerializer());

        /*
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        template.setValueSerializer(new GeneralObjectSerializer());
        template.setHashValueSerializer(new GeneralObjectSerializer());*/

        // template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }
}
